package com.yxw.timeDemo;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigDecimal;

/**
 * server端网络IO事件处理
 * @author xwalker
 *
 */
public class TimeServerHandler extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("服务器读取到客户端请求...");
        Request request = (Request) msg;
        User user2 = request.getRequest();
        System.out.println("the time server receive order:"+user2.getUserName());
        User user = new User();
        user.setUserId(new BigDecimal(1));
        user.setUserName("YXW");
        user.setCardId("360732199010012545");
        user.setMobile("13556567972");
        Response response = new Response();
        response.setResult(user);
        ctx.writeAndFlush(response);
        System.out.println("服务器做出了响应");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
        System.out.println("服务器readComplete 响应完成");
        ctx.close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        ctx.close();
        System.out.println("服务器异常退出"+cause.getMessage());
    }
}
